// pages/user/acti/userImg/userImg.js
import { getFileType } from '../../../utils/file';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    scrollTop: 0,
    actiList: [],
    currActiId: null,
    userImgList: [],
  },
  getScrollHeight() {
    // 获取内容区域的高度
    wx.createSelectorQuery().selectViewport().scrollOffset((res) => {
      this.setData({ scrollHeight: res.scrollHeight + 'px' });
    }).exec()
  },
  onScroll(event) {
    let isScrollEnd = false;
    if (event.detail.scrollTop > 10) {
      isScrollEnd = true;
    }
    if (isScrollEnd != this.data.isScrollEnd) {
      this.setData({ isScrollEnd: isScrollEnd })
    }
    this.setData({ scrollTop: event.detail.scrollTop });
  },
  // Tab页变动
  onChangeTab(event) {
    this.setData({ currActiId: event.detail.name })
    this.queryUserImgList();
    this.getScrollHeight();
  },
  queryActiList() {
    getApp().callContainer('/api/actiQuery/list', {}, (res) => {
      if (res.data.data.actiList != null) {
        this.setData({ actiList: res.data.data.actiList, currActiId: res.data.data.actiList[0].actiId });
      }
    })
  },
  queryUserImgList() {
    const param = {
      actiId: this.data.currActiId
    }
    getApp().callContainer('/api/actiQuery/queryUserImg', param, (res) => {
      if (res.data.code == 0) {
        const userImgList = [];
        for (const userImg of res.data.data.userImgList) {
          const imgUrlList = userImg.imgUrlList.split(',');
          for (const imgUrl of imgUrlList) {
            const type = getFileType(imgUrl);
            userImgList.push({ ...userImg, imgUrl: imgUrl, type: type });
          }
        }

        for (let index = 0; index < userImgList.length; index++) {
          const imgUrl = userImgList[index];
          getApp().getTempFilePath(imgUrl.imgUrl).then((res) => {
            imgUrl.url = res;
            if (index == userImgList.length - 1) {
              this.setData({ userImgList: userImgList });
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options) {
      this.setData({ currActiId: options.actiId })
      this.queryActiList();
      this.queryUserImgList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})